﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyVideoViewerLibrary;


namespace MyVideoViewerManager
{
    /// <summary>
    /// Interaction logic for EpisodeControl.xaml
    /// </summary>
    public partial class EpisodeControl : UserControl
    {
        private MyVideoViewerManagerBusiness bl = new MyVideoViewerManagerBusiness();
        private Episode episode;
        public Episode Episode
        {
            get { return episode; }
            set
            {
                episode = value;
                DataContext = episode;
                if (episode == null)
                {
                    mainGrid.Visibility = Visibility.Collapsed;
                    return;
                }
                else
                    mainGrid.Visibility = Visibility.Visible;

                if (value.FirstAired.HasValue)
                    firstAiredTextBlock.Text = episode.FirstAired.Value.ToString("MMMM d, yyyy");
                else
                    firstAiredTextBlock.Text = "";
                value.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(value_PropertyChanged);
            }
        }

        void value_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "Watched")
                MyVideoViewerBusiness.VideoViewerContext.SubmitChanges();
        }

        public EpisodeControl()
        {
            InitializeComponent();
            chooseFileButton.Click += new RoutedEventHandler(chooseFileButton_Click);
            mainGrid.Drop += new DragEventHandler(mainGrid_Drop);

            bl.AddFileToSeriesCompleted += new EventHandler(bl_AddFileToSeriesCompleted);
            bl.DataUpdated += new UpdateEvent(bl_DataUpdated);
            bl.DataError += new UpdateEvent(bl_DataError);
        }

        void bl_DataError(string updateText)
        {
            MessageBox.Show(updateText);
        }

        void bl_DataUpdated(string updateText)
        {
            statusControl.Update(updateText);
        }

        void bl_AddFileToSeriesCompleted(object sender, EventArgs e)
        {
            statusControl.SetDisplay(false);
        }

        void mainGrid_Drop(object sender, DragEventArgs e)
        {
            if (e.Data is System.Windows.DataObject && ((System.Windows.DataObject)e.Data).ContainsFileDropList())
            {
                foreach (string filePath in ((System.Windows.DataObject)e.Data).GetFileDropList())
                {
                    AddFile(filePath);
                }
            }
        }

        void chooseFileButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Videos"; // Default file name

            // Show open file dialog box
            bool? result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                AddFile(dlg.FileName);
            }
        }

        void AddFile(string filename)
        {
            statusControl.Update("Adding File",true,0);
            statusControl.SetDisplay(true);
            bl.AddFileToSeriesAsync(ref episode, filename);
        }
    }
}
